package org.dromara.app.config;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.app.cssUtils.RedisServiceUtils;
import org.dromara.app.domain.SkYueJu;
import org.dromara.app.domain.vo.SkAppUserVo;
import org.dromara.app.mapper.SkAppUserMapper;
import org.dromara.common.redis.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@RequiredArgsConstructor
@Service
public class AppGoEasyUtil {

    @Value("${goEasy.restHost}")
    private  String gpeasyUrl;

    @Value("${goEasy.app-key}")
    private  String gpeasyAppKey;

    private final SkAppUserMapper skAppUserMapper;



    /**
     * 订阅群
     */
    @Async
    public  void createQun(SkYueJu skYueJu, Long userId,Long fzUserId){
        String url = gpeasyUrl+"subscribe-groups";
        JSONObject jsonParam = new JSONObject();
        String[] userList= new String[]{userId+""};
        String[] groupIdList= new String[]{skYueJu.getId()+""};
        Map<String,Object> map = new HashMap<>();

        jsonParam.put("appkey", gpeasyAppKey);
        jsonParam.put("userIds", userList);
        jsonParam.put("groupIds",groupIdList);
        String response = HttpUtil.createPost(url)
            .body(jsonParam.toString()) // 设置请求体
            .contentType("application/json") // 设置Content-Type
            .execute()
            .body(); // 获取响应内容
        System.out.println(JSONUtil.parse(response).getByPath("code"));
        createQun(skYueJu,userId);
    }

    /**
     * 发送消息
     */
    public  void createQun(SkYueJu skYueJu,Long fzUserId){
        String url = gpeasyUrl+"message";
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("appkey",gpeasyAppKey);
        jsonParam.put("senderId",fzUserId.toString());
        //用户信息
        SkAppUserVo app = RedisUtils.getCacheMapValue(RedisServiceUtils.getUserInfo(),fzUserId+"");
        if (app==null){
            app = skAppUserMapper.selectVoById(fzUserId);
            RedisUtils.setCacheMapValue(RedisServiceUtils.getUserInfo(),fzUserId+"",app);
        }
        JSONObject senderData = new JSONObject();
        senderData.put("avatar",app.getHeadImage()==null?"":app.getHeadImage());
        senderData.put("nickname",app.getNickName());
        jsonParam.put("senderData",senderData);
        JSONObject to = new JSONObject();
        to.put("type","group");
        to.put("id",skYueJu.getId().toString());
        JSONObject todata = new JSONObject();
        todata.put("avatar",skYueJu.getGroupHeadImage());
        todata.put("name",skYueJu.getTitle());
        to.put("data",todata);
        jsonParam.put("to",to);
        jsonParam.put("type","text");
        jsonParam.put("payload","大家好~");
        JSONObject notification = new JSONObject();
        notification.put("title","大家好~");
        notification.put("body","你有一条新消息");
        jsonParam.put("notification",notification);
        String response = HttpUtil.createPost(url)
            .body(jsonParam.toString()) // 设置请求体
            .contentType("application/json") // 设置Content-Type
            .execute()
            .body(); // 获取响应内容
        System.out.println(JSONUtil.parse(response).getByPath("code"));
    }

    /**
     * 取消订阅
     */
    public void dissolveGroupChat(String[] userList,Long yuejuId) {
        String url = gpeasyUrl+"unsubscribe-groups";
        JSONObject jsonParam = new JSONObject();
        String[] groupIdList = new String[]{yuejuId+""};
        jsonParam.put("appkey", gpeasyAppKey);
        jsonParam.put("userIds", userList);
        jsonParam.put("groupIds",groupIdList);
        String response = HttpUtil.createPost(url)
            .body(jsonParam.toString()) // 设置请求体
            .contentType("application/json") // 设置Content-Type
            .execute()
            .body(); // 获取响应内容
        System.out.println(JSONUtil.parse(response).getByPath("code"));
    }
}
